//
// Created by v on 19-7-4.
//

#include "CMBaseProcess.h"

CMBaseProcess::CMBaseProcess(const char *sername) { m_serverName = sername; }

int CMBaseProcess::waitNext(int status, unsigned int inter)
{
    m_nextStatus = status;
    m_lastTime   = cm_gettime_milli();
    m_interval   = inter;
    statusChanged(STATUS_WAIT_NEXT);
    return 0;
}

int CMBaseProcess::statusChanged(int status)
{
    m_lastStatus = m_status;
    m_status     = status;
    CMLOG("[%s] status changed [%d => %d]\n", m_serverName.c_str(),
          m_lastStatus, m_status);
    return 0;
}

int CMBaseProcess::run_general()
{
    switch (m_status)
    {
    case STATUS_WAIT_NEXT:
    {
        run_in_wait();
        unsigned long long now = cm_gettime_milli();
        if (now - m_lastTime > (unsigned long long)m_interval)
        {
            statusChanged(m_nextStatus);
        }
    }
    break;
    default:
        break;
    }
    return 0;
}